﻿<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php //check if userid set
	if (!isset($_GET['userid'])) {
		$to = "client.php";
		redirect_to($to);
		exit;  
		};
 ?>
<?php //check if nuobaudosid set
	if (!isset($_GET['nuobaudosid'])) {
		$nuobaudosid = 0;  
		}else{
		$nuobaudosid = ($_GET['nuobaudosid']);
		//gaunam nuobaudos detales
		$query = "SELECT * 
			FROM `nuobaudos`
			WHERE nuobaudosid=";
		$query .=($_GET['nuobaudosid']);
		$result_set = mysql_query($query, $connection);
		$sabotage = mysql_num_rows($result_set);
		$getnuobauda = mysql_fetch_array($result_set);
		//tikrinam ar yra tokia nuobauda ir ar ji priklauso tam klientui
		if(($sabotage==0) or (($getnuobauda['userid'])!=($_GET['userid']))){echo "<h1>Sabotage<h1>";};
		
		};
 ?>

<?php //check if user has primary photo

	$query = "SELECT *";
	$query .= " FROM `foto`";
	$query .= " WHERE `foto`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= " AND `pagrindine` = 1";
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);
	$subject = mysql_fetch_array($result_set);
	
	if (($subject['pagrindine']) !=0) {
	$message = "Pagrindine foto jau yra";
	$pagridine_yra = 1;
	$foto_location = $subject['path'];
	$foto_id = $subject['fotoid'];
	
	} else {
	$message = "Pagrindines foto nera";
	$pagridine_yra = 0;
	};
	
	
?>

<?php //Gaunam Pagrindinius kliento duomenys
	
	$query = "SELECT *";
	$query .= " FROM `user`, `lovos`";
	$query .= " WHERE `user`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= "AND `user`.lovosid = `lovos`.lovosid";
	
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);

	$subject = mysql_fetch_array($result_set);

?>

 <?php //procesinam ka gaunam is post
 
	if (isset($_POST['submit'])) {
	echo "Submited <br>";
	echo $subject['vardas'];
	echo $subject['burys'];
	if($nuobaudosid==0){
		//naujas nuo cia
			if ((($_POST['nuobauda']) == 3) or (($_POST['nuobauda']) == 4) or (($_POST['nuobauda']) == 5) or (($_POST['nuobauda']) == 7)){
				if($_POST['trukme_tipas'] ==0) {
					$to = "problem.php";
					redirect_to ($to);
				};
			};
		
			if ((($_POST['pazeidimas']) ==0)or (($_POST['nuobauda']) ==0)){
			$to = "problem.php";
			redirect_to ($to);
			};

			
			$sql = "INSERT INTO `nuobaudos` (`userid`, `pazeidrusysid`, `nuobrusysid`, `burys`, `pazeiddata`, `pazeidlaikas`, `pazeidregnr`, `svarstdata`";
			if ((($_POST['nuobauda']) == 3) or (($_POST['nuobauda']) == 4) or (($_POST['nuobauda']) == 5) or (($_POST['nuobauda']) == 7)){
				if ($_POST['trukme_tipas'] ==1){
					$sql .= ", `kiekisdien`";
				}elseif($_POST['trukme_tipas'] ==2){
					$sql .= ", `kiekismen`";
				};
			};
			$sql .= ") VALUES (";
			$sql .= $subject['userid'] . ", ";
			$sql .= $_POST['pazeidimas'] . ", ";
			$sql .= $_POST['nuobauda'] . ", ";
			$sql .= $subject['burys'] . ", ";
			$sql .= "'".$_POST['pazeidimodata'] . "', ";
			$sql .= "'".$_POST['pazeidimolaikas'] . "', ";
			$sql .= $_POST['regnr'] . ", ";
			$sql .= "'".$_POST['svarstymodata']."'";
			if ((($_POST['nuobauda']) == 3) or (($_POST['nuobauda']) == 4) or (($_POST['nuobauda']) == 5) or (($_POST['nuobauda']) == 7)){
			$sql .= ", ".($_POST['trukme']);
			};
			$sql .= ");";
			
			$result = mysql_query($sql, $connection);
				if ($result) {
				
		//===========================================================//
		//   HZ kur redirect
		//===========================================================//
				
				
					$to = "summary.php?userid=" . $_GET['userid'];
					redirect_to($to);
					// Success!
					//$message .= "</br>Client updated Successsullyy!! </br>";
				} else {
					// Display error message.
					$message .= "<p>Client update failed.</p>";
					$message .= "<p>" . mysql_error() . "</p>";
				};
		//naujas iki cia	
		}else{
		//seno redagavimas nuo cia
		if ((($_POST['nuobauda']) == 3) or (($_POST['nuobauda']) == 4) or (($_POST['nuobauda']) == 5) or (($_POST['nuobauda']) == 7)){
				if($_POST['trukme_tipas'] ==0) {
					$to = "problem.php";
					redirect_to ($to);
				};
			};
		
			if ((($_POST['pazeidimas']) ==0)or (($_POST['nuobauda']) ==0)){
			$to = "problem.php";
			redirect_to ($to);
			};

			
			$sql = "UPDATE `nuobaudos`";
			// " (`userid`, `pazeidrusysid`, `nuobrusysid`, `burys`, `pazeiddata`, `pazeidlaikas`, `pazeidregnr`, `svarstdata`";
			// if ((($_POST['nuobauda']) == 3) or (($_POST['nuobauda']) == 4) or (($_POST['nuobauda']) == 5) or (($_POST['nuobauda']) == 7)){
				// if ($_POST['trukme_tipas'] ==1){
					// $sql .= ", `kiekisdien`";
				// }elseif($_POST['trukme_tipas'] ==2){
					// $sql .= ", `kiekismen`";
				// };
			// };
			$sql .= " SET";
			$sql .= " `userid`=";
			$sql .= $subject['userid'] . ", ";
			$sql .= " `pazeidrusysid`=";
			$sql .= $_POST['pazeidimas'] . ", ";
			$sql .= " `nuobrusysid`=";
			$sql .= $_POST['nuobauda'] . ", ";
			$sql .= " `burys`=";
			$sql .= $subject['burys'] . ", ";
			$sql .= " `pazeiddata`=";
			$sql .= "'".$_POST['pazeidimodata'] . "', ";
			$sql .= " `pazeidlaikas`=";
			$sql .= "'".$_POST['pazeidimolaikas'] . "', ";
			$sql .= " `pazeidregnr`=";
			$sql .= $_POST['regnr'] . ", ";
			$sql .= " `svarstdata`=";
			$sql .= "'".$_POST['svarstymodata']."'";
			if ($_POST['trukme_tipas'] ==1){
					$sql .= ", `kiekisdien`=";
				}elseif($_POST['trukme_tipas'] ==2){
					$sql .= ", `kiekismen`=";
				};
			
			if ((($_POST['nuobauda']) == 3) or (($_POST['nuobauda']) == 4) or (($_POST['nuobauda']) == 5) or (($_POST['nuobauda']) == 7)){
			$sql .= ($_POST['trukme']);
			};
			$sql .= " WHERE `nuobaudosid`=";
			$sql .= $_GET['nuobaudosid'];
			
			$result = mysql_query($sql, $connection);
				if ($result) {
				
		//===========================================================//
		//   HZ kur redirect
		//===========================================================//
				
				
					$to = "form_preprot.php?svarstdata=" . $_POST['svarstymodata'];
					redirect_to($to);
					// Success!
					// $message .= "</br>Client uuuupdated Successsullyy!! </br>";
				} else {
					// Display error message.
					$message .= "<p>Client update failed.</p>";
					$message .= "<p>" . mysql_error() . "</p>";
				};
		
		//seno redagavimas iki cia
		};
	};
	
  

 ?>


					<article>
						<h2><?php 
							if($nuobaudosid !=0){
								echo "Nubaudimo redagavimas";
							}else{
								echo "Nubaudimo formavimas";
							};
						
						?></h2>
	<?php echo $message."</br>";?>
	<?php //rodomi pagrindiniai kliento duomenys

	//print_r ($subject) ;
	
	//echo "Vardas: ";
	echo $subject['vardas'];
	echo ' ';
	
	// echo "Pavarde: ";
	echo $subject['pavarde'];
	echo ' ';	
	
	//echo "Bylos Nr.: ";
	echo $subject['bylosnr'];
	echo '</br>';
	$tau_dabar = $subject['tautybeid'];
	$pil_dabar = $subject['pilietybeid'];
	
?>
    <?php //rodom pagrindine foto jeigu yra
	if ($pagridine_yra == 1){ 
		echo "</br> <img src=\"";
		echo $foto_location;
		echo "\" width=\"150\">";
	};
?> 
   
<form action="form_bausti.php?userid=<?php echo ($_GET['userid']);
									 if($nuobaudosid !=0){echo "&nuobaudosid=".$nuobaudosid;};
									 ?>" method="post" name="submit">
  
  </br>
  Svarstymo data: <input name="svarstymodata" type="date" value="<?php 
							if($nuobaudosid !=0){
								echo ($getnuobauda['svarstdata']);
							}else {echo date("Y-m-j");};
						
						?>" placeholder="1900-12-31" required/>
 <br>
  Pazeidimo data: <input name="pazeidimodata" type="date" value="<?php 
							if($nuobaudosid !=0){
								echo ($getnuobauda['pazeiddata']);
							};
						
						?>" placeholder="1900-12-31" required/>
<br>
  Pazeidimo laikas:<input name="pazeidimolaikas" type="time" value="<?php 
							if($nuobaudosid !=0){
								echo ($getnuobauda['pazeidlaikas']);
							};
						
						?>" placeholder="12:20" required step="300"/>
<br>
  Pazeidimo reg.nr:<input name="regnr" type="text" value="<?php 
							if($nuobaudosid !=0){
								echo ($getnuobauda['pazeidregnr']);
							};
						
						?>" placeholder="0000" required/>
<br>
  Pazeidimo vieta: <select name="vieta" size="1">
	<option value="3"> Gyvenamoje zonoje
	</option>
	<option value="1"> Darbo objektuose įstaigos ribose
	</option>
	<option value="2"> Darbo objektuose už įstaigos ribų
	</option>
	

</select>
<br>
  Pazeidimas: <select name="pazeidimas" size="1">
			<?php 		$query = "SELECT * 
						FROM `pazeidimurusys` 
						ORDER BY pazeidrusysid ASC";
						$pazeid_set = mysql_query($query, $connection);
						confirm_query($pazeid_set);
						while ($pazeid = mysql_fetch_array($pazeid_set)) {
							echo "<option value=\"". $pazeid["pazeidrusysid"] . "\"";
													
							if($nuobaudosid !=0){
								if(($getnuobauda['pazeidrusysid'])==($pazeid["pazeidrusysid"])){
								echo " selected";
								};
							};
												
							echo ">" . $pazeid["pazeidrusys"] . "</option>";
						};

			?> <option value="0">Kita</option>
			</select>
<br>
  Paskirta nuobauda: <select name="nuobauda" size="1">
			<?php 		$query = "SELECT * 
						FROM `nuobaudosrusys` 
						ORDER BY nuobrusysid ASC";
						$nuob_set = mysql_query($query, $connection);
						confirm_query($nuob_set);
						while ($nuob = mysql_fetch_array($nuob_set)) {
							echo "<option value=\"". $nuob["nuobrusysid"] . "\"";
													
							if($nuobaudosid !=0){
								if(($getnuobauda['nuobrusysid'])==($nuob["nuobrusysid"])){
								echo " selected";
								};
							};
							
							echo ">" . $nuob["nuobrusys"] . "</option>";
						};

			?> <option value="0">Kita</option>
			</select>			

</br>
Nuobaudos trume: <input name="trukme" type="text" value="<?php 
							if($nuobaudosid !=0){
								if(($getnuobauda['kiekisdien'])!=0){
								echo ($getnuobauda['kiekisdien']);
								$d=1;
								} elseif (($getnuobauda['kiekismen'])!=0){
								echo ($getnuobauda['kiekismen']);
								$m=1;
								};
							};
						
						?>" /> 
					<select name="trukme_tipas" size="1">
					<option value="0"></option>
					<option value="1" <?php
										if($d==1){echo " selected";};
										?>>parų</option>
					<option value="2" <?php
										if($m==1){echo " selected";};
										?>>mėn.</option>
					</select> <small>Pildoma uždarant į BI, uždraudžiant pirkti produktus, perkeliant į drausmės grupę ar KTP</small>		
<br>
<input name="submit" type="submit" value="SubmiNt" />
</form>


			</article>
            
<?php require("includes/footer.php"); ?>
